{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from geoscilabs.dcip import DCLayers\n",
    "from IPython.display import display\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from matplotlib import rcParams\n",
    "rcParams['font.size'] = 14"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Purpose\n",
    "\n",
    "## Investigating DC Resistivity Data \n",
    "\n",
    "Using the widgets contained in this notebook we will explore the physical principals governing DC resistivity including the behavior of currents, electric field, electric potentials in a two layer earth. \n",
    "\n",
    "The measured data in a DC experiment are potential differences, we will demonstrate how these  provide information about subsurface physical properties. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Background: Computing Apparent Resistivity\n",
    "\n",
    "In practice we cannot measure the potentials everywhere, we are limited to those locations where we place electrodes. For each source (current electrode pair) many potential differences are measured between M and N electrode pairs to characterize the overall distribution of potentials. The widget below allows you to visualize the potentials, electric fields, and current densities from a dipole source in a simple model with 2 layers. For different electrode configurations you can measure the potential differences and see the calculated apparent resistivities. \n",
    "\n",
    "In a uniform halfspace the potential differences can be computed by summing up the potentials at each measurement point from the different current sources based on the following equations:\n",
    "\n",
    "\\begin{align}\n",
    "    V_M = \\frac{\\rho I}{2 \\pi} \\left[ \\frac{1}{AM} - \\frac{1}{MB} \\right] \\\\\n",
    "    V_N = \\frac{\\rho I}{2 \\pi} \\left[ \\frac{1}{AN} - \\frac{1}{NB} \\right] \n",
    "\\end{align} \n",
    "where $AM$, $MB$, $AN$, and $NB$ are the distances between the corresponding electrodes. \n",
    "\n",
    "The potential difference $\\Delta V_{MN}$ in a dipole-dipole survey can therefore be expressed as follows,\n",
    "\n",
    "\\begin{equation}\n",
    "    \\Delta V_{MN} = V_M - V_N = \\rho I \\underbrace{\\frac{1}{2 \\pi} \\left[ \\frac{1}{AM} - \\frac{1}{MB} - \\frac{1}{AN} + \\frac{1}{NB} \\right]}_{G}\n",
    "\\end{equation}\n",
    "\n",
    "and the resistivity of the halfspace $\\rho$ is equal to,\n",
    "\n",
    "$$\n",
    "    \\rho = \\frac{\\Delta V_{MN}}{IG}\n",
    "$$\n",
    "\n",
    "In this equation $G$ is often referred to as the geometric factor. \n",
    "\n",
    "In the case where we are not in a uniform halfspace the above equation is used to compute the apparent resistivity ($\\rho_a$) which is the resistivity of the uniform halfspace which best reproduces the measured potential difference.\n",
    "\n",
    "In the top plot the location of the A electrode is marked by the red +, the B electrode is marked by the blue -, and the M/N potential electrodes are marked by the black dots. The $V_M$ and $V_N$ potentials are printed just above and to the right of the black dots. The calculted apparent resistivity is shown in the grey box to the right. The bottom plot can show the resistivity model, the electric fields (e), potentials, or current densities (j) depending on which toggle button is selected. Some patience may be required for the plots to update after parameters have been changed."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# LayeredEarth app\n",
    "\n",
    "## Parameters:\n",
    "\n",
    " - **A**: (+) Current electrode  location\n",
    " - **B**: (-) Current electrode  location\n",
    " - **M**: (+) Potential electrode  location\n",
    " - **N**: (-) Potential electrode  location\n",
    " - **$\\rho_1$**: Resistivity of the first layer\n",
    " - **$\\rho_2$**: Resistivity of the second layer\n",
    " - **h**: Thickness of the first layer\n",
    " - **Plot**: Choice of 2D plot (Model, Potential, Electric field, Currents)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "b22738ea4c464a98bb81a71e0f62f3b6",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "MyApp(children=(FloatSlider(value=-30.0, continuous_update=False, description='A', max=40.0, min=-40.0, step=1…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "out = DCLayers.plot_layer_potentials_app()\n",
    "display(out)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  },
  "widgets": {
   "state": {
    "d1e777f115314ea497dca383d1bc4d52": {
     "views": [
      {
       "cell_index": 4
      }
     ]
    }
   },
   "version": "1.2.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
